package za.org.jcicada.genss.lifecycle.impl;

import za.org.jcicada.comms.IConnection;
import za.org.jcicada.comms.impl.MessagingBase;
import za.org.jcicada.genss.SystemURN;
import za.org.jcicada.genss.lifecycle.ILifeCycleClient;
import za.org.jcicada.genss.lifecycle.ILifeCycleServer;
import za.org.jcicada.genss.lifecycle.LogLevel;
import za.org.jcicada.genss.lifecycle.messages.HaltReply;
import za.org.jcicada.genss.lifecycle.messages.PingReply;
import za.org.jcicada.genss.lifecycle.messages.ReconnectReply;
import za.org.jcicada.genss.lifecycle.messages.RestartReply;
import za.org.jcicada.genss.lifecycle.messages.SetLogLevelReply;
import za.org.jcicada.genss.messages.Result;

public class LifeCycleMessaging extends MessagingBase {

    /**
     * Handle to life-cycle server for invoking methods on.
     */
    ILifeCycleServer lifeCycleServer;

    /**
     * Handle to callback object for messages from the server to the client.
     */
    ILifeCycleClient lifeCycleClient;

    public LifeCycleMessaging(IConnection connection, ILifeCycleServer lifeCycleServer) {
        super(connection);
        this.lifeCycleServer = lifeCycleServer;
        lifeCycleClient = new Responder();
    }

    public void onClose() throws Exception {
        lifeCycleServer.informDisconnect(lifeCycleClient);
    }

    /**
     * Dispatch HaltRequest.
     */
    public void lifecycleHaltRequest(za.org.jcicada.genss.lifecycle.messages.HaltRequest msg) {
        lifeCycleServer.requestHalt(lifeCycleClient, msg.getSystemURN());
    }

    /**
     * Dispatch RestartRequest.
     */
    public void lifecycleRestartRequest(za.org.jcicada.genss.lifecycle.messages.RestartRequest msg) {
        lifeCycleServer.requestRestart(lifeCycleClient, msg.getSystemURN());
    }

    /**
     * Dispatch SetLogLevelRequest.
     */
    public void lifecycleSetLogLevelRequest(za.org.jcicada.genss.lifecycle.messages.SetLogLevelRequest msg) {
        lifeCycleServer.requestSetLogLevel(lifeCycleClient, msg.getSystemURN(), msg.getLevel());
    }
    
    /**
     * Dispatch ReconnectRequest.
     */
    public void lifecycleReconnectRequest(za.org.jcicada.genss.lifecycle.messages.ReconnectRequest msg) {
        lifeCycleServer.requestReconnect(lifeCycleClient, msg.getSystemURN());
    }
    
    public void lifecyclePingRequest(za.org.jcicada.genss.lifecycle.messages.PingRequest msg) {
    	lifeCycleServer.requestPing(lifeCycleClient, msg.getSystemURN());
    }

    /**
     * Implementation of ILifeCycleClient that sends messages back to a katcomms
     * connection.
     */
    public class Responder extends MessagingBase.ResponderBase implements ILifeCycleClient {

        /**
         * Called after requesting a halt using ILifeCycleServer.requestHalt().
         */
        public void replyHalt(Result result, SystemURN systemURN) {
            sendMessage(new HaltReply(result, systemURN));
        }

        /**
         * Called after requesting a restart using ILifeCycleServer.requestRestart().
         */
        public void replyRestart(Result result, SystemURN systemURN) {
            sendMessage(new RestartReply(result, systemURN));
        }
        
        /**
         * Called after setting the log level using ILifeCycleServer.requestSetLogLevel().
         */
        public void replySetLogLevel(Result result, SystemURN systemURN, LogLevel level) {
            sendMessage(new SetLogLevelReply(result, systemURN, level));
        }
        
        public void replyReconnect(Result result, SystemURN systemURN) {
            sendMessage(new ReconnectReply(result, systemURN));
        }
        
        public void replyPing(Result result, SystemURN systemURN) {
        	sendMessage(new PingReply(result, systemURN));
        }

    }
}
